package com.grad.why.studentsystem.controller.qlx;


import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.Teacher;
import com.grad.why.studentsystem.domain.User;
import com.grad.why.studentsystem.domain.qlxdomain.StudentMenu;
import com.grad.why.studentsystem.service.AdminService;
import com.grad.why.studentsystem.service.StudentService;
import com.grad.why.studentsystem.service.TeacherService;
import com.grad.why.studentsystem.service.UserService;
import com.grad.why.studentsystem.service.qlxservice.StudentMenuService;
import com.grad.why.studentsystem.util.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@RequestMapping("/studentsystem")
public class StudentLoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private StudentMenuService studentMenuService;

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/studentlogin")
    public String goToLogin() throws Exception {
        return "qlx/studentlogin";
    }

    @RequestMapping(value="/checkCode")
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("image/jpeg");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);

        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);//输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/checkLogin")
    @ResponseBody
    public User checkLogin(HttpServletRequest request, String username, String password, String code){
        try{
            int n = userService.checkUserIsExistByUserName(username);
            if(n==0){
                return null;
            }else{
                HttpSession session = request.getSession();
                String randCode = (String)session.getAttribute("randomcode_key");
                if(randCode.toLowerCase().equals(code.toLowerCase())){
                    User user = userService.getUserByUserNameAndPassword(username);
                    if(user.getPassword().equals(password)){
                        session.setAttribute("user",user);
                        int type = user.getType();
                        int bindid = user.getBindid();
                        session.setAttribute("type",type);
                        session.setAttribute("bindid",bindid);
                        String infos = "";
                        if(type == 4){
                            Student student = studentService.getStudentInfoBySid(bindid);
                            infos = student.getName()+"("+student.getId()+")";
                           /* infos = student.getName();*/
                        }else if(type == 2){
                            Teacher teacher = teacherService.getTeacherInfoByTid(bindid);
                            infos = teacher.getName()+"("+teacher.getId()+")";
                        }
                        session.setAttribute("infos",infos);
                        return user;
                    }else{
                        return null;
                    }
                }else{
                    return null;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/studentstarter")
    public String goToStarter(HttpServletRequest request, ModelMap modelMap){
        User user = ((User)request.getSession().getAttribute("user"));
        int type = user.getType();
        if(type==4){
            ArrayList<StudentMenu> studentmenuinfos = studentMenuService.getStudentMenuInfos();
            modelMap.addAttribute("studentmenuinfos",studentmenuinfos);
            return "qlx/studentStarter";
        }

        else
            return "qlx/teacherStarter";
    }

    @RequestMapping("/signOut")
    public String signOut(HttpServletRequest request){
        try{
            HttpSession session = request.getSession();
            session.removeAttribute("user");
            session.removeAttribute("infos");
            session.removeAttribute("type");
            session.removeAttribute("bindid");
        }catch (Exception e){
            e.printStackTrace();
        }
        return "qlx/studentlogin";
    }


}
